<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="ykf">
    <title>child练习</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
</html>     
<script>
    function child(name,isMale,birthdate){
        this.name = name;
        this.isMale = isMale;
        this.birthdate = birthdate;
    }
    child.prototype.eat = function(food){
        console.log(this.name+'吃了'+food);
    };
    child.prototype.sleep = function(hours){
        console.log(this.name+'睡了'+hours+'个小时');
    }
    child.prototype.play = function(game){
        console.log(this.name+'玩'+game);
    }

    var tom = new child('Tom','true','1995');

    Object.defineProperty(tom,'age',{
        set:function(val){
            if(val<0){
                return;
            }
            var today = new Date();
            this.birthdate = today.getFullYear() - val;
        },
        get:function(){
            var today = new Date();
            return today.getFullYear() - this.birthdate;
        }
    })

    var age = tom.age;

    tom.eat('香蕉');
    tom.sleep('16');
    tom.play('游戏');

    console.log(tom.name);
    console.log('age='+age);
    console.log(tom.isMale);
    console.log(tom.birthdate);
</script>